﻿package  {
	import flash.display.ColorCorrection;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;

	/** This class implements a clickable button that may change in color and/or text as 
	*	user mouses-over and presses-down.
	*/
	public class MyButton extends MovieClip{
		//private static const ENABLED_COLOR:uint = 0x000000;
		//private static const DISABLED_COLOR:uint = 0xAAAAAA;
		public var fontSize:Number;
		private var _enabled:Boolean,_selected:Boolean, remainSelected:Boolean;
		private var upColor:uint, overColor:uint, downColor:uint, backColor:uint;
		private var upTextColor:uint, overTextColor:uint, downTextColor:uint;
		private var upText:String, overText:String, downText:String;
		private var tField:TextField;
		private var faceWidth:Number, faceHeight:Number;
		private var ellipseWidth:Number, ellipseHeight:Number;
		private var face:MovieClip,back:MovieClip;
		private var is_mouseOver:Boolean = false;
		/** Include the width, and height of the intended button (with the option to expand for text), set text and color for states of up, over, down 
			If user wants to use exact size of largest text use WIDTH=0, HEIGHT=0
		*/
		public function MyButton(WIDTH:Number, HEIGHT:Number, fontSize:Number, ellipseWidth:Number, ellipseHeight:Number, upColor:uint = 0xFFFFFF, upText:String = "", upTextColor:uint=0x444444, overColor:uint = 0xCCCCCC, overText:String = "", overTextColor:uint=0x444444, downColor:uint = 0xAAAAAA, downText:String = "", downTextColor:uint=0x444444, resizeForText:Boolean=true, beginEnabled:Boolean = true, remainSelected:Boolean = true){
			this.upColor = upColor;
			this.overColor = overColor;
			this.downColor = downColor;
			this.upText = upText;
			this.overText = overText;
			this.downText = downText;
			this.upTextColor = upTextColor;
			this.overTextColor = overTextColor;
			this.downTextColor = downTextColor;
			this.faceWidth = WIDTH;
			this.faceHeight = HEIGHT;
			this.ellipseHeight = ellipseHeight;
			this.ellipseWidth = ellipseWidth;
			this.remainSelected = remainSelected;
			var maxString:String = upText;
			var r:int, g:int, b:int;
			r = upColor & 0xFF0000; r = r >> 16;
			g = upColor & 0x00FF00; g = g >> 8;
			b = upColor & 0x0000FF;
			var dr:int = r-r/4; if (dr < 0) dr = 0;
			var dg:int = g-g/4; if (dg < 0) dg = 0;
			var db:int = b-b/4; if (db < 0) db = 0;
			backColor = dr << 16 | dg << 8 | db;
			
			if (upText < overText){
				maxString = overText;
			} 
			if (maxString < downText){
				maxString = downText;
			}
			this.fontSize = fontSize;
			tField = new TextField();
			tField.selectable = false;
			tField.mouseEnabled = false;
			if (beginEnabled){
				tField.defaultTextFormat = new TextFormat("Arial",fontSize,upTextColor);
			} else {
				tField.defaultTextFormat = new TextFormat("Arial",fontSize,(upTextColor+upColor)/2);
			}
			tField.text = maxString;
			tField.autoSize = "left";
			
			// setup face
			face = new MovieClip();
			//face.graphics.lineStyle(1,0x000000);
			face.graphics.beginFill(upColor);
			if (resizeForText){
				// if button is too small for text, resize
				if (WIDTH < tField.width && HEIGHT < tField.height){
					face.graphics.drawRoundRect(0,0,tField.width,tField.height,ellipseWidth,ellipseHeight);
				} else if (WIDTH < tField.width){
					face.graphics.drawRoundRect(0,0,tField.width,HEIGHT,ellipseWidth,ellipseHeight);
				} else if (HEIGHT < tField.height){
					face.graphics.drawRoundRect(0,0,WIDTH,tField.height,ellipseWidth,ellipseHeight);
				} else {
					face.graphics.drawRoundRect(0,0,WIDTH,HEIGHT,ellipseWidth,ellipseHeight);
				}
			}
			face.graphics.endFill();
			faceWidth = face.width;
			faceHeight = face.height;
			
			// Now that we have resized, use the real text (up) and center on button
			tField.text = upText;
			tField.autoSize = "left";
			tField.x = (face.width - tField.width)/2;
			tField.y = (face.height - tField.height)/2;
			
			back = new MovieClip();
			back.graphics.beginFill(backColor);
			var shadowExtra:Number = Math.min(face.width/14, face.height/14);
			back.graphics.drawRoundRect(shadowExtra/2,shadowExtra/2,face.width+shadowExtra,face.height+shadowExtra,ellipseWidth,ellipseHeight);
			
			this.addChild(back);
			face.addChild(tField);
			this.addChild(face);	
			//Event Listener
			if (beginEnabled){ 
				enable();
			} 
		}
		/*
		public function adjustSize(newWidth:int, newHeight:int):void
		{
			back.graphics.beginFill(backColor);
			back.graphics.drawRoundRect(0,0,newWidth,newHeight,ellipseWidth,ellipseHeight);
			
			face.graphics.beginFill(upColor);
			face.graphics.drawRoundRect(0,0,back.width-back.width/16, back.height-back.width/16,ellipseWidth,ellipseHeight);
			faceWidth = face.width;
			faceHeight = face.height;
			
			tField.x = (face.width - tField.width)/2;
			tField.y = (face.height - tField.height)/2;
		}
		*/
		public function get text():String
		{
			return this.tField.text; 
		}
		public function set text(s:String):void{this.tField.text = s;}
		
		
		/** When enabled place face on button, place mouse event on face */
		public function enable():void
		{
			if (!_enabled)
			{
				tField.defaultTextFormat = new TextFormat("Arial",20,upTextColor);
				face.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
				_enabled = true;
			}
		}
		/** NEEDS WORK */
		public function disable():void
		{
			if (_enabled)
			{
				tField.defaultTextFormat = new TextFormat("Arial",20,(upTextColor+upColor)/2);
				face.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
				_enabled = false;
			}
		}
		/** Remotely selects this button */
		public function select():void
		{
			if (!_enabled) enable();
			_selected = true;
			face.graphics.clear();
			//replace color and text
			//face.graphics.lineStyle(1,0x000000);
			face.graphics.beginFill(downColor);
			face.graphics.drawRoundRect(0,0,faceWidth,faceHeight,ellipseWidth,ellipseHeight);
			face.graphics.endFill();
			tField.defaultTextFormat = new TextFormat("Arial",fontSize,upTextColor);
			tField.text = downText;
			tField.autoSize = "left";
			tField.x = (face.width - tField.width)/2;
			tField.y = (face.height - tField.height)/2;
		}
		/** Remotely de-selects this button */
		public function deselect():void
		{
			_selected = false;
			face.graphics.clear();
			//replace color and text
			//face.graphics.lineStyle(1,0x000000);
			face.graphics.beginFill(upColor);
			face.graphics.drawRoundRect(0,0,faceWidth,faceHeight,ellipseWidth,ellipseHeight);
			face.graphics.endFill();
			tField.defaultTextFormat = new TextFormat("Arial",fontSize,(upTextColor+upColor)/2);
			tField.text = upText;
			tField.autoSize = "left";
			tField.x = (face.width - tField.width)/2;
			tField.y = (face.height - tField.height)/2;
			//place new listeners on face
			if (is_mouseOver){
				face.addEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);
				face.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
			} else {
				face.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
			}
		}
		
		private function handleMouseOver(event:MouseEvent):void
		{
			face.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
			if (!_selected || !remainSelected){
				face.graphics.clear();
				is_mouseOver = true;
				//replace color and text
				//face.graphics.lineStyle(1,0x000000);
				face.graphics.beginFill(overColor);
				face.graphics.drawRoundRect(0,0,faceWidth,faceHeight,ellipseWidth,ellipseHeight);
				face.graphics.endFill();
				tField.defaultTextFormat = new TextFormat("Arial",fontSize,overTextColor);
				tField.text = overText;
				tField.autoSize = "left";
				tField.x = (face.width - tField.width)/2;
				tField.y = (face.height - tField.height)/2;
			}
			//place new listeners on face
			face.addEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);
			face.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
		}
		private function handleMouseOut(event:MouseEvent):void
		{
			face.removeEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);
			face.removeEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
			if (!_selected || !remainSelected){
				face.graphics.clear();
				is_mouseOver = false;
				//replace color and text
				//face.graphics.lineStyle(1,0x000000);
				face.graphics.beginFill(upColor);
				face.graphics.drawRoundRect(0,0,faceWidth,faceHeight,ellipseWidth,ellipseHeight);
				face.graphics.endFill();
				tField.defaultTextFormat = new TextFormat("Arial",fontSize,upTextColor);
				tField.text = upText;
				tField.autoSize = "left";
				tField.x = (face.width - tField.width)/2;
				tField.y = (face.height - tField.height)/2;
			}
			//place new listeners on face
			face.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
		}
		private function handleMouseDown(event:MouseEvent):void
		{
			//face.removeEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);
			face.removeEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
			face.graphics.clear();
			//replace color and text
			//face.graphics.lineStyle(1,0x000000);
			face.graphics.beginFill(downColor);
			face.graphics.drawRoundRect(0,0,faceWidth,faceHeight,ellipseWidth,ellipseHeight);
			face.graphics.endFill();
			tField.defaultTextFormat = new TextFormat("Arial",fontSize,downTextColor);
			tField.text = downText;
			tField.autoSize = "left";
			tField.x = (face.width - tField.width)/2;
			tField.y = (face.height - tField.height)/2;
			//place new listeners on face
			face.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
			face.addEventListener(MouseEvent.MOUSE_OUT, handleMouseOutWhileDown);
		}
		/** Cancel this press */
		private function handleMouseOutWhileDown(event:MouseEvent):void
		{
			face.removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
			face.removeEventListener(MouseEvent.MOUSE_OUT, handleMouseOutWhileDown);
			face.graphics.clear();
			is_mouseOver = false;
			//replace color and text
			//face.graphics.lineStyle(1,0x000000);
			face.graphics.beginFill(upColor);
			face.graphics.drawRoundRect(0,0,faceWidth,faceHeight,ellipseWidth,ellipseHeight);
			face.graphics.endFill();
			tField.defaultTextFormat = new TextFormat("Arial",fontSize,upTextColor);
			tField.text = upText;
			tField.autoSize = "left";
			tField.x = (face.width - tField.width)/2;
			tField.y = (face.height - tField.height)/2;
			//place new listeners on face
			face.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
		}

		private function handleMouseUp(event:MouseEvent):void
		{
			face.removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
			face.removeEventListener(MouseEvent.MOUSE_OUT, handleMouseOutWhileDown);
			_selected = true;
			this.dispatchEvent(new Event(Event.COMPLETE));
			// IS THE MOUSE OVER THE BUTTON, CHECK AND SEE WHETHER WE SHOULD PUT AN OUT OR AN OVER LISTENER
			face.graphics.clear();
			face.graphics.beginFill(overColor);
			face.graphics.drawRoundRect(0,0,faceWidth,faceHeight,ellipseWidth,ellipseHeight);
			face.graphics.endFill();
			tField.defaultTextFormat = new TextFormat("Arial",fontSize,overTextColor);
			tField.text = overText;
			tField.autoSize = "left";
			tField.x = (face.width - tField.width)/2;
			tField.y = (face.height - tField.height)/2;
			face.addEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);
			
		}
	}
}	

	
